package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author Login
 * @since 2025-07-17
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private ICategoryService categoryService;

    @Override
    public List<CategoryBrand> selectCategoryBrandlList(CategoryBrand categoryBrand) {

        return baseMapper.selectCategoryBrandlList(categoryBrand);

    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        //根据brandId和categoryId 与新增品牌进行对比，查询新增品牌是否存在。
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        Long count = baseMapper.selectCount(queryWrapper);
        if(count>0){
            throw new RuntimeException("该分类下已有该品牌");
        }
        return baseMapper.insert(categoryBrand);

    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        // 1. 根据ID查询基础数据
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        // 2. 获取关联分类ID  列表
        List<Long>  categoryIdList = categoryService.getAllCategoryList(categoryBrand.getCategoryId());   //因为是在Category表中查询的，所以要引入categoryService进行业务处理
        // 3. 设置分类ID列表
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        //判断分类id和品牌id是否发生了变化
        CategoryBrand categoryBrand1 = baseMapper.selectById(categoryBrand.getId());

        if(categoryBrand1.getCategoryId().equals(categoryBrand.getCategoryId())
        && categoryBrand1.getBrandId().equals(categoryBrand.getBrandId())){
            // categoryBrand1 和 categoryBrand 的id相等。表示没有发生改变，直接返回。默认就是返回成功
            return 1;
        }


        //如果记录发生变化，则判断是否和其他记录重复
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        if(baseMapper.selectCount(queryWrapper)>0){
            throw new RuntimeException("该该分类下已有该品牌");
        }
        return baseMapper.updateById(categoryBrand);
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {

        return baseMapper.selectBrandListByCategoryId(categoryId);
    }


}
